package com.sgb.knowledgeshare.weight;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.aliyun.common.utils.DensityUtil;
import com.aliyun.player.IPlayer;
import com.sgb.knowledgeshare.view.adapter.RecommendAdapter;
import com.swgk.core.base.model.entity.VideoBean;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by：冯齐 on 2020/12/6 14:50
 * Description：
 */
public class AlivcVideoPlayView extends FrameLayout {
    private Context context;
    private LoadingView mLoadingView;
    private AlivcVideoListView videoListView;
    private RecommendAdapter mVideoAdapter;
    private AlivcVideoListView.OnRefreshDataListener onRefreshDataListener;
    private BaseVideoListAdapter adapter;

    public AlivcVideoPlayView(@NonNull Context context) {
        this(context, null);
    }

    public AlivcVideoPlayView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    private void init() {
        initPlayListView();
        initLoadingView();
    }

    //列表view处理
    private void initPlayListView() {
        videoListView = new AlivcVideoListView(context);

        //创建adapter，需要继承BaseVideoListAdapter
        mVideoAdapter = new RecommendAdapter(context);
        mVideoAdapter.setCallback(new RecommendAdapter.VideoCallback() {
            @Override
            public void setControllerView(ControllerView controller, VideoBean videoBean) {
                callback.UserVideoController(controller,videoBean);
            }

            @Override
            public void onPlayOrPause() {
                videoListView.onPlayOrPause();
            }

            @Override
            public void VideoLike(VideoBean bean,ControllerView controller) {
                //视频点赞
                callback.UserVideoLike(bean,controller);
            }

        });
        //给AlivcVideoListView实例化对象添加adapter
        videoListView.setAdapter(mVideoAdapter);
        videoListView.setVisibility(VISIBLE);
        //设置sdk播放器实例化对象数量
        videoListView.setPlayerCount(1);
        //设置下拉、上拉监听进行加载数据
        videoListView.setOnRefreshDataListener(new AlivcVideoListView.OnRefreshDataListener() {
            @Override
            public void onRefresh() {
                if (onRefreshDataListener != null) {
                    onRefreshDataListener.onRefresh();
                }
            }

            @Override
            public void onLoadMore() {
                if (onRefreshDataListener != null) {
                    onRefreshDataListener.onLoadMore();
                }
            }
        });
        //设置视频缓冲监听
        videoListView.setLoadingListener(new IPlayer.OnLoadingStatusListener() {
            @Override
            public void onLoadingBegin() {
                mLoadingView.start();
            }

            @Override
            public void onLoadingEnd() {
                mLoadingView.cancle();
            }

            @Override
            public void onLoadingProgress(int var1, float var2) {

            }
        });
        //添加到布局中
        addSubView(videoListView);
        videoListView.setCallback(new AlivcVideoListView.PlayVideoListInfo() {
            @Override
            public void VideoInfo(int pos,VideoBean video) {
                callback.VideoInfo(pos,video);
            }


        });
    }

    /**
     * addSubView 添加子view到布局中
     *
     * @param view 子view
     */
    private void addSubView(View view) {
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                FrameLayout.LayoutParams.MATCH_PARENT);
        addView(view, params);
    }

    //视频加载loadingview处理
    private void initLoadingView() {
        mLoadingView = new LoadingView(context);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
                5);
        params.setMargins(0, 0, 0, DensityUtil.dip2px(getContext(), 4));
        params.gravity = Gravity.BOTTOM;
        addView(mLoadingView, params);
    }


    /**
     * 设置下拉刷新数据listener
     *
     * @param listener OnRefreshDataListener
     */
    public void setOnRefreshDataListener(AlivcVideoListView.OnRefreshDataListener listener) {
        this.onRefreshDataListener = listener;
    }

    /**
     * 刷新视频列表数据
     *
     * @param datas
     */
    public void refreshVideoList(List<? extends VideoBean> datas) {
        List<VideoBean> videoList = new ArrayList<>();
        videoList.addAll(datas);
        videoListView.refreshData(videoList);
        //取消加载loading
        mLoadingView.cancle();
    }
    /**
     * 刷新视频列表数据
     *
     * @param datas
     * @param position
     */
    public void refreshVideoList(List<VideoBean> datas, int position) {
        List<VideoBean> videoList = new ArrayList<>();
        videoList.addAll(datas);
        videoListView.refreshData(videoList, position);
        //取消加载loading
        mLoadingView.cancle();
    }

    /**
     * 添加更多视频
     *
     * @param datas
     */
    public void addMoreData(List<? extends VideoBean> datas) {
        List<VideoBean> videoList = new ArrayList<>();
        videoList.addAll(datas);
        videoListView.addMoreData(videoList);
        //取消加载loading
        mLoadingView.cancle();
    }
    /**
     * 视频列表获取失败
     */
    public void loadFailure() {
        mLoadingView.cancle();
        videoListView.loadFailure();
    }

    private UserVideoCallback callback;
    public void setCallback(UserVideoCallback callback) {
        this.callback = callback;
    }

  /**
     * 获取list数据大小
     * @return
     */
    public List<VideoBean> getAdapterList() {
        if (mVideoAdapter!=null){
            List<VideoBean> dataList = mVideoAdapter.getDataList();
            if (dataList!=null&&dataList.size()>0){
                return dataList;
            }else {
                return new ArrayList<>();
            }
        }else {
            return new ArrayList<>();
        }
    }

    /**
     * 调用恢复视频播放
     */
    public void setRecoverVideoPlay() {
        if (videoListView!=null){
            videoListView.setRecoverVideoPlay();
        }
    }

    /**
     * 调用暂停视频播放
     */
    public void setPauseVideoPlay() {
        if (videoListView!=null){
            videoListView.setPauseVideoPlay();
        }
    }

    /**
     * 设置视频播放位置
     * @param i
     */
    public void setPlayVideoIndex(String i) {
        if (videoListView!=null){
            videoListView.setPlayVideoIndex(i);
        }
    }

    /**
     * 初始化视频播放源
     * @param videoBean
     */
    public void setVideoList(List<VideoBean> videoBean) {
        if (videoListView!=null){
            videoListView.setVideoList(videoBean);
        }
    }

    /**
     * 是否可以垂直滑动
     * @param scroll
     */
    public void onCanVerticalScroll(boolean scroll) {
        if (videoListView!=null){
            videoListView.onCanVerticalScroll(scroll);
        }
    }

    /**
     * 控制右边按钮是否显示
     * @param b
     */
    public void setShowUser(boolean b) {
        if (mVideoAdapter!=null){
            mVideoAdapter.setShowUser(b);
        }
    }

    public void setShowComment(boolean b) {
        if (mVideoAdapter!=null){
            mVideoAdapter.setShowComment(b);
        }
    }


    public interface UserVideoCallback {
        void UserVideoLike(VideoBean bean,ControllerView controller);
        void UserVideoController(ControllerView controller, VideoBean videoBean);
        void VideoInfo(int pos,VideoBean video);
    }

    public void onStart() {

    }

    public void onResume() {
        videoListView.setOnBackground(false);

    }

    public void onStop() {
        mLoadingView.cancle();
    }

    public void onPause() {

        videoListView.setOnBackground(true);

    }

    public void onDestroy() {
        context = null;
    }

}
